package com.thinkmore.es.test.bean;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;

public class EsConfig {

    public static String indexName = "file7";

    public static RestHighLevelClient init() {
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials("elastic", "WmI4WHbJuY0N*L4BzUHF");
        credentialsProvider.setCredentials(AuthScope.ANY, usernamePasswordCredentials);

        RestClientBuilder builder = RestClient.builder(new HttpHost("tudou-Deepin", 9200))
                .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
                .setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder.setConnectTimeout(5000).setSocketTimeout(60000));

        return new RestHighLevelClient(builder);
    }
}
